import { defineConfig, loadEnv } from 'vite'
import path from 'node:path'
import uni from '@dcloudio/vite-plugin-uni'
import AutoImport from 'unplugin-auto-import/vite'
import UnoCSS from 'unocss/vite'
import UniManifest from '@uni-helper/vite-plugin-uni-manifest'
import UniComponents from '@uni-helper/vite-plugin-uni-components'
import { NutResolver } from 'nutui-uniapp'
import DefineOptions from 'unplugin-vue-define-options/vite'
import { console } from 'node:inspector'

// https://vitejs.dev/config/
export default ({ command, mode }) => {
  const env = loadEnv(mode, path.resolve(process.cwd()))
  const { VITE_APP_API_BASEURL, VITE_APP_PROXY, VITE_APP_PROXY_PREFIX } = env
  return defineConfig({
    resolve: {
      alias: {
        '@': path.join(process.cwd(), './src'),
        '@img': path.join(process.cwd(), './src/static/imgs')
      }
    },
    plugins: [
      UniComponents({
        dts: false,
        resolvers: [NutResolver()]
      }),
      UniManifest(),
      uni(),
      AutoImport({
        dts: false,
        imports: [
          'vue',
          'uni-app',
          'pinia',
          {
            '@/api': ['useRequest']
          }
        ],
        eslintrc: { enabled: true }
      }),
      UnoCSS(),
      DefineOptions()
    ],
    // server: {
    //   host: '0.0.0.0',
    //   hmr: true,
    //   // 仅 H5 端生效，其他端不生效（其他端走build，不走devServer)
    //   proxy: JSON.parse(VITE_APP_PROXY)
    //     ? {
    //         [VITE_APP_PROXY_PREFIX]: {
    //           target: VITE_APP_API_BASEURL,
    //           changeOrigin: true,
    //           rewrite: (path) => path.replace(new RegExp(`^${VITE_APP_PROXY_PREFIX}`), '')
    //         }
    //       }
    //     : undefined
    // },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "nutui-uniapp/styles/variables.scss";`
        }
      }
    }
  })
}
